package com.fzy.modules.bill.web;

import com.fzy.core.common.ApiController;
import com.fzy.core.common.ResponseData;
import com.fzy.modules.bill.entity.SoBill;
import com.fzy.modules.bill.service.SoBillService;
import com.fzy.modules.people.service.CustomerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 销售订单Controller层
 */
@RestController
@RequestMapping(value = "/api/bill/so")
public class SoBillController extends ApiController {
    @Autowired
    SoBillService soBillService;
    @Autowired
    CustomerService customerService;
    @PostMapping(value = "/list")
    public ResponseEntity<ResponseData> list(SoBill soBill,
     @RequestParam(value = "page", defaultValue = "1") int page,
     @RequestParam(value = "rows", defaultValue = "10") int rows) {
        return new ResponseEntity<>(success(soBillService.findAllSoBill(soBill, (page), rows)), HttpStatus.OK);
    }
    @PostMapping(value = "")
    public ResponseEntity<ResponseData> save(SoBill soBill) {
      if (StringUtils.isNotEmpty(soBill.getId())) {
            return new ResponseEntity<>(success(soBillService.checkOut(soBill.getBillStatus(),soBill.getId())), HttpStatus.OK);
        }
        return new ResponseEntity<>(success(soBillService.save(soBill)), HttpStatus.OK);
    }
    @PostMapping(value = "/findAllSoBill")
    public List<SoBill> findAllSoBill(){
        return soBillService.findAllSoBill();
    }
    /**
     * 分页查询本用户指定状态的订单信息
     * @param inputUserId 制单人id
     * @param page
     * @param rows
     * @param billStatus 订单状态
     * @return
     */
    @GetMapping(value = "/findSoBillByInputUserId")
    public ResponseEntity<ResponseData> findSoBillByInputUserId(
    		String inputUserId,
    		 @RequestParam(value = "page", defaultValue = "1") int page,
    	     @RequestParam(value = "rows", defaultValue = "10") int rows,
    	     Integer billStatus ){
    	System.out.println(inputUserId);
    	System.out.println(billStatus);
    	System.out.println(rows);
    	if(billStatus ==null||inputUserId==null){
    		return new ResponseEntity<>(fail("参数不正确!"), HttpStatus.BAD_GATEWAY);
    	}
        return new ResponseEntity<>(success(soBillService.findSoBillByInputUserId(inputUserId,billStatus,page,rows)), HttpStatus.OK);
    }
   
} 
